package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.inmail.core.domain.aggregatemodel.SafetyEducationVo;
import com.smedi.ismedi.inmail.core.domain.dto.SafetyEducationQuery;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.service.SafetyEducationRemoteApi;
import com.smedi.ismedi.safety.education.api.dto.request.SafetyEducationListRequest;
import com.smedi.ismedi.safety.education.api.dto.response.SafetyEducationResponse;
import com.smedi.ismedi.safety.education.api.feign.RemoteSafetyEducationFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@AllArgsConstructor
@Service
public class SafetyEducationRemoteApiImpl implements SafetyEducationRemoteApi {
    private final RemoteSafetyEducationFeignApi remoteSafetyEducationFeignApi;
    private final AdapterConverter adapterConverter;

    public List<SafetyEducationVo> getSafetyEducationList(SafetyEducationQuery safetyEducationQuery){
        SafetyEducationListRequest safetyEducationListRequest = adapterConverter.safetyEducationQueryToSafetyEducationListRequest(safetyEducationQuery);
        ResponseData<List<SafetyEducationResponse>> responseData = remoteSafetyEducationFeignApi.getSafetyEducationList(safetyEducationListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return adapterConverter.safetyEducationResponseToSafetyEducation(responseData.getData());
    }

    @Override
    public Integer getSafetyEducationCount(SafetyEducationQuery safetyEducationQuery) {
        SafetyEducationListRequest safetyEducationListRequest = adapterConverter.safetyEducationQueryToSafetyEducationListRequest(safetyEducationQuery);
        ResponseData<Integer> responseData = remoteSafetyEducationFeignApi.getSafetyEducationCount(safetyEducationListRequest);
        if (!responseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, responseData.getApplication(), null != responseData.getErrorMsg() ? responseData.getErrorMsg().toString() : null);
        }
        return responseData.getData();
    }
}
